package com.autolinq.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import com.autolinq.pojo.WeChatMessage;
import com.autolinq.util.http.HttpPostMessage;

public class WechatDeviceEventService extends AbstractWechatService{

	@Override
	public String handleWechatMessage(WeChatMessage message,
			String qrcodePath) {
		if(message.getEvent().toLowerCase().equals("bind")){
			HttpPostMessage.postMessage(prop.getProperty("bindURL"), "openId="+message.getOpenID()+"&deviceId="+message.getDeviceID());
		}
		
		
		String returnStr = "";

		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

		String times = format.format(new Date());
		String timeLong = String.valueOf(new Date().getTime());
		times = timeLong.substring(0, timeLong.length()-3);

		Element rootXML = new Element("xml");

		rootXML.addContent(new Element("ToUserName").setText(message.getFromUserName()));

		rootXML.addContent(new Element("FromUserName").setText(message.getToUserName()));

		rootXML.addContent(new Element("CreateTime").setText(times));

		rootXML.addContent(new Element("MsgType").setText("device_event"));
		
		rootXML.addContent(new Element("Event").setText(message.getEvent()));
		
		rootXML.addContent(new Element("DeviceType").setText(message.getDeviceType()));

		rootXML.addContent(new Element("DeviceId").setText(message.getDeviceID()));

		rootXML.addContent(new Element("SessionId").setText(message.getSessionID()));
		
		rootXML.addContent(new Element("Content").setText(message.getContent()));
		
		rootXML.addContent(new Element("OpenID").setText(message.getOpenID()));
		
		Document doc = new Document(rootXML);

		XMLOutputter XMLOut = new XMLOutputter();

		returnStr = XMLOut.outputString(doc);

		return returnStr;
	}
	
}